/*
 */

package com.railway.system.action;

import java.util.HashMap;
import java.util.Map;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.railway.common.base.BaseStruts2Action;
import com.railway.common.util.Json;
import com.railway.system.model.SysRole;
import com.railway.system.service.ISysRoleManager;
import com.railway.system.vo.query.SysRoleQuery;

/**
 * @author  jiangcheng
 * @version 1.0
 * @since 1.0
 */

public class SysRoleAction extends BaseStruts2Action implements Preparable,
		ModelDriven {
	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null;

	//forward paths
	protected static final String QUERY_JSP = "/admin/system/sysRole/query.jsp";
	protected static final String LIST_JSP = "/admin/system/sysRole/list.jsp";

	protected static final String CREATE_JSP = "/admin/system/sysRole/create.jsp";

	protected static final String EDIT_JSP = "/admin/system/sysRole/edit.jsp";
	protected static final String SHOW_JSP = "/admin/system/sysRole/show.jsp";

	//redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/system/SysRole/list.do";

	private ISysRoleManager sysRoleManager;

	private SysRole sysRole;
	java.lang.String id = null;
	private String[] items;
	private String param;

	public void setParam(String param) {
		this.param = param;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			sysRole = new SysRole();
		} else {
			sysRole = (SysRole) sysRoleManager.getById(id);
		}
	}

	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setSysRoleManager(ISysRoleManager manager) {
		this.sysRoleManager = manager;
	}

	public Object getModel() {
		return sysRole;
	}

	public void setId(java.lang.String val) {
		this.id = val;
	}

	public void setItems(String[] items) {
		this.items = items;
	}

	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}

	public String listAjax() {
		
		SysRoleQuery query = newQuery(SysRoleQuery.class, DEFAULT_SORT_COLUMNS);
		
		Page page = sysRoleManager.findPage(query);
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		
		return writeAjaxResponse(Json.toJson(map));
		
	}

	/** 查看对象*/
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面*/
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
		sysRoleManager.save(sysRole);
		return LIST_ACTION;
	}

	/**进入更新页面*/
	public String edit() {
		return EDIT_JSP;
	}

	/**保存更新对象*/
	public String update() {
		sysRoleManager.update(this.sysRole);
		return LIST_ACTION;
	}

	/**删除对象*/
	public String delete() {
		if (items != null)
			for (String ids : items) {
				String[] idArray = ids.split(",");
				for (String id : idArray)
					sysRoleManager.removeById(id);
			}
		return LIST_ACTION;
	}

}
